// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify it
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

/**
 * Created by eran on 14-12-21.
 */
package data
{
    public class DataVoManger
    {
        private static var _instance:DataVoManger;

        public static function get instance():DataVoManger
        {
            if (_instance == null)
            {
                _instance = new DataVoManger();
            }
            return _instance;
        }

        private var mAllBlockVoArray:Array = [];

        private var mMapVo:MapVo;

        public function DataVoManger()
        {
            mMapVo = new MapVo();
        }

        //=================================//
        //====== Public Function ==========//
        //=================================//

        //====================
        //== BlockVo相关
        //====================

        public function get allBlockVoArray():Array {return mAllBlockVoArray;}

        public function addBlockVo(_blockVo:BlockVo):void {mAllBlockVoArray.push(_blockVo);}

        public function deleteBlockVo(_blockVo:BlockVo):void
        {
            var index:int = mAllBlockVoArray.indexOf(_blockVo);
            if (index >= 0)
            {
                mAllBlockVoArray.splice(index, 1);
            }
            trace(mAllBlockVoArray.length);
        }

        public function findBlockByTile(_tileIndexX:int, _tileIndexY:int):BlockVo
        {
            for each(var blockVo:BlockVo in mAllBlockVoArray)
            {
                if (_tileIndexX >= blockVo.x && _tileIndexX < blockVo.x + blockVo.width && _tileIndexY >= blockVo.y &&
                    _tileIndexY < blockVo.y + blockVo.height)
                {
                    return blockVo;
                }
            }
            return null;
        }


        //======================
        //== JSON 序列化与反序列化
        //======================
        public function fromJSON(_jsonObj:Object):void
        {
            mAllBlockVoArray.length = 0;
            var blockVoArray:Array = _jsonObj["blockVoArray"];
            for each(var blockVoObj:Object in blockVoArray)
            {
                var blockVo:BlockVo = new BlockVo();
                blockVo.fromJSON(blockVoObj);
                mAllBlockVoArray.push(blockVo);
            }

            mMapVo.fromJSON(_jsonObj["mapVo"]);
        }

        public function toJSONString():String
        {
            var saveObj:Object = {
                blockVoArray: mAllBlockVoArray,
                mapVo       : mMapVo
            }
            return JSON.stringify(saveObj);
        }


        //===============
        //== MapVo
        //===============

        public function get mapVo():MapVo {return mMapVo;}


    }
}
